package com.android.myapplication.view;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.android.myapplication.MainActivity;
import com.android.myapplication.R;
import com.android.myapplication.main.MainContent;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.appcompat.widget.Toolbar;

import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;

import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An activity representing a single Item detail screen. This
 * activity is only used on narrow width devices. On tablet-size devices,
 * item details are presented side-by-side with a list of items
 * in a {@link MainActivity}.
 */
public class TextActivity extends AppCompatActivity {
    public static final String ARG_ITEM_ID = "item_id";
    private MainContent.MainItem mItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
        setSupportActionBar(toolbar);

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
////                        .setAction("Action", null).show();
//            }
//        });

        // Show the Up button in the action bar.
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
// Show the dummy content as text in a TextView.
        mItem = MainContent.ITEM_MAP.get(getIntent().getStringExtra(ARG_ITEM_ID));

        if (mItem != null) {
            ((TextView) findViewById(R.id.item_detail)).setText(mItem.details);
        }




        TextView textView = findViewById(R.id.tv_span);
        SpannableString spanString = new SpannableString("字体颜色 字体背景 字体风格 超链接 图片: ");

        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);//字体颜色红色
        ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);//字体颜色红色
        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.YELLOW);//背景颜色黄色
        StyleSpan styleSpan = new StyleSpan(Typeface.BOLD_ITALIC);//粗斜体
        URLSpan urlSpan = new URLSpan("http://www.baidu.com");


        spanString.setSpan(redSpan, 0 ,2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanString.setSpan(blueSpan, 2 ,3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spanString.setSpan(backgroundColorSpan, 5 ,9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanString.setSpan(styleSpan, 10 ,15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanString.setSpan(urlSpan, 15 ,18, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        Drawable d = getResources().getDrawable(R.drawable.icon_1);
        d.setBounds(0, 0, 80, 80);

        ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        spanString.setSpan(imageSpan, 21, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView.setText(spanString);


        final EditText editText = findViewById(R.id.ed_text);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("AAA", "onTextChanged = " + s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
                if("123456".equals(s.toString())){
                    Toast.makeText(TextActivity.this, "你输入123456，密码正确", Toast.LENGTH_LONG).show();
                    editText.setTextColor(Color.BLUE);
                }else{
                    editText.setTextColor(Color.RED);
                }

            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            navigateUpTo(new Intent(this, MainActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
